package net.huashitong.interview.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.interview.entity.EnterpriseFuWu;
import net.huashitong.interview.entity.InterviewRegister;
import net.huashitong.interview.entity.NeedSolveProblem;
import net.huashitong.interview.filter.EnterpriseFuWuFilter;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EnterpriseFuWuDao extends StringDao<EnterpriseFuWu> {

    public List<EnterpriseFuWu> queryFuWu(EnterpriseFuWuFilter filter){

        StringBuffer sql = new StringBuffer("select " +
                "t.sys_id as sysId," +
                "t.help_enterprise as helpEnterprise," +
                "t.specific_problem as specificProblem," +
                "t.solve_schedule as solveSchedule," +
                "t.fill_date as fillDate," +
                "t.fill_unit_id as fillUnitId," +
                "t.next_step as nextStep," +
                "t.remark as remark," +
                "t.state as state " +
                "from t_gis_interview_enterprise_service t where 1=1");

        if(ObjectUtils.isNotBlank(filter.getFillUnitId())){
            sql.append(" and t.fill_unit_id ='"+filter.getFillUnitId()+"'");
        }
        if(ObjectUtils.isNotBlank(filter.getFillDate())){
            sql.append(" and t.fill_date like '"+filter.getFillDate()+"%'");
        }
        sql.append(" order by t.fill_date desc");

        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(EnterpriseFuWu.class));
        List<EnterpriseFuWu> list  = (List<EnterpriseFuWu>)query.list();
        return list;

    }

    public String getNewFillDate(){
        GenericQuery query = createQuery("obj");
        query.setOrder("fillDate",BaseFilter.DESC);
        EnterpriseFuWu register = query.firstResult();
        if(ObjectUtils.isBlank(register)){
            return "0000";
        }
        return register.getFillDate();
    }


}
